package com.example.taximeter.service.impl;

import com.example.taximeter.bean.*;
import com.example.taximeter.mapper.PrivilegeMapper;
import com.example.taximeter.mapper.RoleMapper;
import com.example.taximeter.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色
 */
@Service
public class RoleServiceImpl implements RoleService {

    private RoleMapper roleMapper;
    private PrivilegeMapper privilegeMapper;

    @Autowired
    public RoleServiceImpl (RoleMapper roleMapper,PrivilegeMapper privilegeMapper){
        this.roleMapper = roleMapper;
        this.privilegeMapper = privilegeMapper;
    }

    @Override
    public List<Role> findRolelist() {
        return roleMapper.findRoleList();
    }

    @Override
    public String addRole(Role role) {


        if (roleMapper.addRole(role)) {
            if(role.getPrivilegeidList()!=null&&role.getPrivilegeidList().size()!=0) {
                for (int i = 0; i < role.getPrivilegeidList().size(); i++) {
                    Integer p = role.getPrivilegeidList().get(i);
                    Integer r = role.getRoleid();
                    if (!roleMapper.addRolePrivilege(r, p)) {
                        return "失败";
                    }
                }
                if (!role.getPrivilegeidList().contains(4)) {
                    for (int i = 0; i < role.getPrivilegeidList().size(); i++) {
                        Integer p = role.getPrivilegeidList().get(i);
                        Integer r = role.getRoleid();
                        if (p == 5 || p == 6 || p == 7) {
                            if (!roleMapper.addRolePrivilege(r, 4)) {
                                return "失败";
                            }
                            break;
                        }
                    }
                }
                if (!role.getPrivilegeidList().contains(8)) {
                    for (int i = 0; i < role.getPrivilegeidList().size(); i++) {
                        Integer p = role.getPrivilegeidList().get(i);
                        Integer r = role.getRoleid();
                        if (p == 9 || p == 10) {
                            if (!roleMapper.addRolePrivilege(r, 8)) {
                                return "失败";
                            }
                            break;
                        }
                    }
                }
//                if (!role.getPrivilegeidList().contains(2)) {
//                    for (int i = 0; i < role.getPrivilegeidList().size(); i++) {
//                        Integer p = role.getPrivilegeidList().get(i);
//                        Integer r = role.getRoleid();
//                        if (p == 18 ) {
//                            if (!roleMapper.addRolePrivilege(r, 2)) {
//                                return "失败";
//                            }
//                            break;
//                        }
//                    }
//                }
            }

            return "成功";
        } else {
            return "失败";
        }

    }

    @Override
    public ReturnInfo findRoleAll(Integer pageNum, Integer pageSize, Role role) {
        if(!pageNum.equals(0)||!pageSize.equals(0)){
            PageHelper.startPage(pageNum, pageSize);
        }
        Integer pagetotal = 0;
        Long total = 0L;
        List<Role> roles = roleMapper.findRoleAll(role);
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        if(roles.size()!=0){
            pagetotal = pageInfo.getPages();
            total = pageInfo.getTotal();
        }
        return ReturnInfo.info5(pagetotal,total,pageInfo.getList());
    }


    @Override
    public String updateRole(Role role) {

        if(roleMapper.updateRole(role)){
            return "成功";
        }else {
            return "失败";
        }

    }

    @Override
    public String deleteRole(Integer roleid) {
        if(!privilegeMapper.deleteRolePrivilege(roleid)){
               return "失败";
           }

        if(roleMapper.deleteRole(roleid)){
            return "成功";
        }else {
            return "失败";
        }
    }

    @Override
    public String updateRolePriv(Role role) {
        if(privilegeMapper.deleteRolePrivilege(role.getRoleid())){
            if(role.getPrivilegeidList()!=null&&role.getPrivilegeidList().size()!=0){
                for(int i =0;i<role.getPrivilegeidList().size();i++){
                    Integer p =  role.getPrivilegeidList().get(i);
                    Integer r =   role.getRoleid();
                    if(!roleMapper.addRolePrivilege(r,p)){
                        return "失败";
                    }
                }
                if (!role.getPrivilegeidList().contains(4)) {
                    for (int i = 0; i < role.getPrivilegeidList().size(); i++) {
                        Integer p = role.getPrivilegeidList().get(i);
                        Integer r = role.getRoleid();
                        if (p == 5 || p == 6 || p == 7) {
                            if (!roleMapper.addRolePrivilege(r, 4)) {
                                return "失败";
                            }
                            break;
                        }
                    }
                }
                if (!role.getPrivilegeidList().contains(8)) {
                    for (int i = 0; i < role.getPrivilegeidList().size(); i++) {
                        Integer p = role.getPrivilegeidList().get(i);
                        Integer r = role.getRoleid();
                        if (p == 9 || p == 10) {
                            if (!roleMapper.addRolePrivilege(r, 8)) {
                                return "失败";
                            }
                            break;
                        }
                    }

                }
//                if (!role.getPrivilegeidList().contains(2)) {
//                    for (int i = 0; i < role.getPrivilegeidList().size(); i++) {
//                        Integer p = role.getPrivilegeidList().get(i);
//                        Integer r = role.getRoleid();
//                        if (p == 18 ) {
//                            if (!roleMapper.addRolePrivilege(r, 2)) {
//                                return "失败";
//                            }
//                            break;
//                        }
//                    }
//                }
            }
            return "成功";
        }else {
            return "失败";
        }



    }


}
